{
 "cells": [
  {
   "cell_type": "code",
   "source": [
    "!pip install unittest"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-08-05T16:42:35.466514Z",
     "start_time": "2025-08-05T16:41:10.458846Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "ERROR: Could not find a version that satisfies the requirement unittest (from versions: none)\n",
      "ERROR: No matching distribution found for unittest\n"
     ]
    }
   ],
   "execution_count": 1
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [],
   "source": [
    "import hashlib\n",
    "import json\n",
    "import os\n",
    "\n",
    "class UserManager:\n",
    "    \"\"\"用户管理系统：处理用户注册、登录和凭证存储\"\"\"\n",
    "    DATA_FILE = \"users.json\"\n",
    "\n",
    "    def __init__(self):\n",
    "        \"\"\"初始化用户存储文件\"\"\"\n",
    "        self.users = self._load_users()\n",
    "\n",
    "    def _load_users(self):\n",
    "        \"\"\"从文件加载用户数据\"\"\"\n",
    "        if not os.path.exists(self.DATA_FILE):\n",
    "            return {}\n",
    "        try:\n",
    "            with open(self.DATA_FILE, 'r') as f:\n",
    "                return json.load(f)\n",
    "        except json.JSONDecodeError:\n",
    "            return {}\n",
    "\n",
    "    def _save_users(self):\n",
    "        \"\"\"保存用户数据到文件\"\"\"\n",
    "        with open(self.DATA_FILE, 'w') as f:\n",
    "            json.dump(self.users, f, indent=2)\n",
    "\n",
    "    @staticmethod\n",
    "    def _hash_password(password):\n",
    "        \"\"\"使用SHA-256生成密码哈希值\"\"\"\n",
    "        return hashlib.sha256(password.encode('utf-8')).hexdigest()\n",
    "\n",
    "    def register(self, username, password):\n",
    "        \"\"\"注册新用户\"\"\"\n",
    "        if username in self.users:\n",
    "            return False, \"用户名已存在\"\n",
    "\n",
    "        self.users[username] = {\n",
    "            \"password_hash\": self._hash_password(password),\n",
    "            \"login_attempts\": 0\n",
    "        }\n",
    "        self._save_users()\n",
    "        return True, \"注册成功\"\n",
    "\n",
    "    def login(self, username, password):\n",
    "        \"\"\"用户登录验证\"\"\"\n",
    "        user = self.users.get(username)\n",
    "        if not user:\n",
    "            return False, \"用户不存在\"\n",
    "\n",
    "        # 检查登录尝试次数限制\n",
    "        if user.get(\"login_attempts\", 0) >= 3:\n",
    "            return False, \"账户已锁定，请稍后重试\"\n",
    "\n",
    "        # 验证密码\n",
    "        if user[\"password_hash\"] != self._hash_password(password):\n",
    "            user[\"login_attempts\"] = user.get(\"login_attempts\", 0) + 1\n",
    "            self._save_users()\n",
    "            return False, \"密码错误\"\n",
    "\n",
    "        # 重置登录尝试计数器\n",
    "        user[\"login_attempts\"] = 0\n",
    "        self._save_users()\n",
    "        return True, \"登录成功\"\n",
    "\n",
    "    def reset_login_attempts(self, username):\n",
    "        \"\"\"重置用户登录尝试次数（管理员功能）\"\"\"\n",
    "        if username in self.users:\n",
    "            self.users[username][\"login_attempts\"] = 0\n",
    "            self._save_users()\n",
    "            return True\n",
    "        return False"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "data": {
      "text/plain": "(True, '注册成功')"
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from user_auth import UserManager\n",
    "\n",
    "auth_system = UserManager()\n",
    "\n",
    "# 用户注册\n",
    "auth_system.register(\"john\", \"MySecure@Pwd\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [],
   "source": [
    "success, message = auth_system.login(\"john\", \"MySecure@Pwd\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "登录成功: True, 消息: 登录成功\n"
     ]
    }
   ],
   "source": [
    "print(f\"登录成功: {success}, 消息: {message}\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "name": "langchain",
   "language": "python",
   "display_name": "langChain"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
